library(tidycensus)
library(dplyr)
library(tmap)
library(sf)
library(tigris)

options(tigris_use_cache = TRUE)
oslo <-  grDevices::hcl.colors(10, "Oslo")
oslo[1] <- "#EAEDF6"

purple <-  rev(grDevices::hcl.colors(11, "Purples"))

purple[1] <- "#CDCDCD"
purple[2] <- "#FFFFFF"

grays <-  rev(grDevices::hcl.colors(4, "Grays"))

mi_shape <- get_acs(
  geography = "tract", 
  variables = "B19013_001",
  state = "MI", 
  county = c("Genesee","Lapeer", "Tuscola", "Saginaw", "Shiawassee", "Livingston", "Oakland"),
  year = 2020,
  geometry = TRUE
)

fl_shape <- get_acs(
  geography = "county subdivision", 
  variables = "B19013_001",
  state = "MI", 
  county = "Genesee",
  year = 2020,
  geometry = TRUE
)

fl_shape <- filter(fl_shape, GEOID=="2604929000")
mi_shape$GEOID <- gsub("^.{0,2}", "", mi_shape$GEOID)
mi_shape <- mi_shape[!st_is_empty(mi_shape),,drop=FALSE]

gc_base <- filter(mi_shape, grepl(c("Genesee","Tuscola","Lapeer","Oakland","Livingston","Shiawassee","Saginaw"), NAME))
gc_shape <- get_acs(
  geography = "county", 
  variables = "B19013_001",
  state = "MI", 
  county = c("Genesee"),
  year = 2020,
  geometry = TRUE
) %>% st_cast("MULTILINESTRING")


############pre
arrests <- readxl::read_excel('C:/Users/xiy333/Dropbox/IGNITE/2input_data/arrests_pre.xlsx')
arrests$arrests <- as.numeric(arrests$arrests)
arrests$total <- rep(sum(arrests$arrests), times=length(arrests$arrests))
arrests$percent <- (arrests$arrests/arrests$total)*100
test <- merge(arrests, mi_shape, all=T
              # x.all = TRUE, y.all = FALSE
              ) %>%
  st_sf()
test <- test[!st_is_empty(test),,drop=FALSE]
test <- test %>% 
  mutate(percent = coalesce(percent, 0))

test2 <- st_cast(fl_shape,"MULTILINESTRING")
# 
# ####number
# pdf("C:/Users/xiy333/Dropbox/IGNITE/5figures/pre_map_GC.pdf") 
# 
# 
#   tm_shape(test) + 
#   tm_borders(alpha = .3)+
#   tm_polygons(col = "arrests",
#               style = "fixed",
#               palette = oslo,
#               breaks = c(0, 50, 100, 150, 600),
#               labels = c("1-50", 
#                          "51-100", 
#                          "101-150", 
#                          ">150"),
#               title = "")+
#   tm_shape(test2) + 
#   tm_lines(lwd=2,
#            lty="solid",
#            col = "orangered") +
#   tm_shape(gc_shape) + 
#   tm_lines(lwd=3,
#            lty="solid",
#            col = "orangered") +
#   tm_layout(frame = FALSE,
#             legend.position = c("left","top"),
#             legend.text.size = 1)
# 
# dev.off()

####percent
pdf("C:/Users/xiy333/Dropbox/IGNITE/5figures/pre_map_GC_percent.pdf") 

tm_shape(test) + 
  tm_borders(alpha = .3)+
  tm_polygons(col = "percent",
              style = "fixed",
              palette = purple,
              breaks = c(0,.05, .3, .6, .9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7),
              labels = c("0%",
                        "0.0-0.3%", 
                         "0.3-0.6%", 
                         "0.6-0.9%", 
                         "0.9-1.2%", 
                         "1.2-1.5%",
                         "1.5-1.8%",
                         "1.8-2.1%",
                         "2.1-2.4%",
                         ">2.4%"),
              title = "")+
  tm_shape(test2) + 
  tm_lines(lwd=2,
           lty="solid",
           col = "orangered") +
  tm_shape(gc_shape) + 
  tm_lines(lwd=3,
           lty="solid",
           col = "orangered") +
  tm_layout(frame = FALSE,
            legend.position = c("left","top"),
            legend.text.size =.6)

dev.off()

############post


arrests <- readxl::read_excel('C:/Users/xiy333/Dropbox/IGNITE/2input_data/arrests_post.xlsx')
arrests$arrests <- as.numeric(arrests$arrests)
arrests$total <- rep(sum(arrests$arrests), times=length(arrests$arrests))
arrests$percent <- (arrests$arrests/arrests$total)*100
test <- merge(arrests, mi_shape, all=T
              # x.all = TRUE, y.all = FALSE
) %>%
  st_sf()
test <- test[!st_is_empty(test),,drop=FALSE]
test <- test %>% 
  mutate(percent = coalesce(percent, 0))


test2 <- st_cast(fl_shape,"MULTILINESTRING")
# 
# ###number
# pdf("C:/Users/xiy333/Dropbox/IGNITE/5figures/post_map_GC.pdf") 
# 
# tm_shape(test) + 
#   tm_borders(alpha = .3)+
#   tm_polygons(col = "arrests",
#               style = "fixed",
#               palette = oslo,
#               breaks = c(0, 50, 100, 150, 600),
#               labels = c("1-50", 
#                          "51-100", 
#                          "101-150", 
#                          ">150"),
#               title = "")+
#   tm_shape(test2) + 
#   tm_lines(lwd=2,
#            lty="solid",
#            col = "orangered") +
#   tm_shape(gc_shape) + 
#   tm_lines(lwd=3,
#            lty="solid",
#            col = "orangered") +
#   tm_layout(frame = FALSE,
#             legend.position = c("left","top"),
#             legend.text.size =1)
# 
# 
# dev.off()

###percent
pdf("C:/Users/xiy333/Dropbox/IGNITE/5figures/post_map_GC_percent.pdf") 

tm_shape(test) + 
  tm_borders(alpha = .3)+
  tm_polygons(col = "percent",
              style = "fixed",
              palette = purple,
              breaks = c(0,.05, .3, .6, .9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7),
              labels = c("0%",
                         "0.0-0.3%", 
                         "0.3-0.6%", 
                         "0.6-0.9%", 
                         "0.9-1.2%", 
                         "1.2-1.5%",
                         "1.5-1.8%",
                         "1.8-2.1%",
                         "2.1-2.4%",
                         ">2.4%"),
              title = "")+
  tm_shape(test2) + 
  tm_lines(lwd=2,
           lty="solid",
           col = "orangered") +
  tm_shape(gc_shape) + 
  tm_lines(lwd=3,
           lty="solid",
           col = "orangered") +
  tm_layout(frame = FALSE,
            legend.position = c("left","top"),
            legend.text.size =.6)


dev.off()
